package com.aqie.meet;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ArgbEvaluator;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.util.Log;
import android.util.Property;
import android.view.View;

import com.aqie.libcommon.app.BaseActivity;
import com.aqie.libfactory.persistence.Account;
import com.aqie.meet.ui.login.PermissionsFragment;

import net.qiujuer.genius.res.Resource;
import net.qiujuer.genius.ui.compat.UiCompat;

/**
 * @author aqie
 * @date 2021/2/10 18:01
 */
public class LaunchActivity extends BaseActivity {
    private static final String TAG = "LAUNCH";
    // Drawable
    private ColorDrawable mBgDrawable;

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_launch;
    }

    @Override
    protected void initBefore() {
        super.initBefore();
        // 校验权限
        // PermissionsFragment.haveAll(this, getSupportFragmentManager());
    }

    public static void show(Context context) {
        Intent intent = new Intent(context, LaunchActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    // 初始化组件
    @Override
    protected void initWidget() {
        super.initWidget();
        // 拿到根布局
        View root = findViewById(R.id.activity_launch);
        // 获取颜色
        int color = UiCompat.getColor(getResources(), R.color.color_blue);
        // 创建一个Drawable
        ColorDrawable drawable = new ColorDrawable(color);
        // 设置给背景
        root.setBackground(drawable);
        mBgDrawable = drawable;
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (PermissionsFragment.haveAll(this, getSupportFragmentManager())) {
            // 动画进入到50%等待PushId获取到
            startAnim(0.5f, new Runnable() {
                @Override
                public void run() {
                    // 检查等待状态
                    waitPushReceiverId();
                }
            });
        }
    }

    /**
     * 等待推送 框架对我们的PushId设置好值
     */
    private void waitPushReceiverId() {
        if (Account.isLogin()) {
            // 已经登录情况下，判断是否绑定
            skip();
            return;
            // todo 如果没有绑定则等待广播接收器进行绑定
            /*if (Account.isBind()) {
                Log.e(TAG," isBind");
                skip();
                return;
            }*/
        } else {
            // 没有登录
            // 如果拿到了PushId, 没有登录是不能绑定PushId的
            if (!TextUtils.isEmpty(Account.getDeviceId())) {
                // 跳转
                Log.e(TAG,"Not login_IN pushId is " + Account.getDeviceId());
                skip();
                return;
            }
            // 没有拿到推送id也跳转
            Log.e(TAG,"not login, can't get pushID " + Account.getDeviceId());
            skip();
            return;
        }

        // Log.e("TAG","Loop waiting for");
        // 循环等待
        /*getWindow().getDecorView()
                .postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        waitPushReceiverId();
                    }
                }, 500);*/
    }

    @Override
    protected void initData() {
        super.initData();
    }

    /**
     * 在跳转之前需要把剩下的50%进行完成
     */
    private void skip() {
        startAnim(1f, new Runnable() {
            @Override
            public void run() {
                reallySkip();
            }
        });
    }

    /**
     * 真实的跳转
     */
    private void reallySkip() {
        new Handler(Looper.getMainLooper()).post(runnable);
        finish();

    }

    // 跳转到账户界面
    final Runnable runnable = new Runnable(){
        @Override
        public void run() {
            // do complicated operation
            // 检查跳转到主页还是登录
            if (Account.isLogin()) {
                MainActivity.show(getApplicationContext());
            } else {
                // AccountActivity.show(getApplicationContext());
                MainActivity.show(getApplicationContext());
            }
        }
    };


    /**
     * 给背景设置一个属性动画
     *
     * @param endProgress 动画的结束进度
     * @param endCallback 动画结束时触发
     */
    private void startAnim(float endProgress, final Runnable endCallback) {
        // 获取一个最终的颜色
        int finalColor = Resource.Color.WHITE; // UiCompat.getColor(getResources(), R.color.white);
        // 运算当前进度的颜色
        ArgbEvaluator evaluator = new ArgbEvaluator();
        int endColor = (int) evaluator.evaluate(endProgress, mBgDrawable.getColor(), finalColor);
        // 构建一个属性动画
        ValueAnimator valueAnimator = ObjectAnimator.ofObject(this, property, evaluator, endColor);
        valueAnimator.setDuration(1500); // 时间
        valueAnimator.setIntValues(mBgDrawable.getColor(), endColor); // 开始结束值
        valueAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                // 结束时触发
                endCallback.run();
            }
        });
        valueAnimator.start();
    }

    private final Property<LaunchActivity, Object> property = new Property<LaunchActivity, Object>(Object.class, "color") {
        @Override
        public void set(LaunchActivity object, Object value) {
            object.mBgDrawable.setColor((Integer) value);
        }

        @Override
        public Object get(LaunchActivity object) {
            return object.mBgDrawable.getColor();
        }
    };
}
